
package com.swak.frame.spring.configuration;

import com.swak.frame.common.SpringBeanFactory;
import com.swak.frame.eventbus.EventBusConfig;
import com.swak.frame.eventbus.EventHandlerAdapter;
import com.swak.frame.monitor.async.AsyncThreadPoolMonitor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.ThreadPoolExecutor;

@Configuration
@ComponentScan(basePackages = "com.swak.frame")
@EnableAspectJAutoProxy
@Order(Ordered.HIGHEST_PRECEDENCE + 1000)
public class SwakAutoConfiguration {

    @Bean
    @ConditionalOnMissingBean(SpringBeanFactory.class)
    public SpringBeanFactory springBeanFactory() {
        return new SpringBeanFactory();
    }

    @Bean
    @ConditionalOnMissingBean(CommonsMultipartResolver.class)
    public CommonsMultipartResolver multipartResolver() {
        CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
        multipartResolver.setDefaultEncoding("UTF-8");
        multipartResolver.setMaxUploadSize(31 * 1024 * 1024);
        multipartResolver.setMaxInMemorySize(40960);
        return multipartResolver;
    }

    /**
     * 单独的线程池 1,10,100000，超过丢弃
     *
     * @return
     */
    @Bean
    @ConditionalOnMissingBean(EventHandlerAdapter.class)
    public EventHandlerAdapter eventHandlerAdapter(@Autowired(required = false) EventBusConfig eventBusConfig, AsyncThreadPoolMonitor asyncThreadPoolMonitor) {
        ThreadPoolTaskExecutor eventBusPool = new ThreadPoolTaskExecutor();
        eventBusConfig = Optional.ofNullable(eventBusConfig).orElse(EventBusConfig.defaultConf());
        eventBusPool.setCorePoolSize(eventBusConfig.getCorePoolSize());// 当前线程数
        eventBusPool.setMaxPoolSize(eventBusConfig.getMaxPoolSize());// 最大线程数
        eventBusPool.setQueueCapacity(eventBusConfig.getQueueCapacity());
        eventBusPool.setWaitForTasksToCompleteOnShutdown(true);
        eventBusPool.setAwaitTerminationSeconds(60 * 10);// 等待时间 （默认为0，此时立即停止），并没等待xx秒后强制停止
        eventBusPool.setThreadNamePrefix("EventBus-threadPool-");
        eventBusPool.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardOldestPolicy());
        eventBusPool.initialize(); // 初始化
        EventHandlerAdapter eventBusAdapter = new EventHandlerAdapter(eventBusPool);
        if(Objects.nonNull(asyncThreadPoolMonitor)) {
            asyncThreadPoolMonitor.startup(eventBusPool);
        }
        return eventBusAdapter;
    }
}
